﻿#region Using directives
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Linq;

using ProcolDemo.I18N;
using ProcolDemo.Ratings;
using ProcolDemo.User;
using ProcolDemo.MyTeamsReference;
using Reimers.Map;

#endregion

namespace ProcolDemo {
    public partial class MyTeams : Page {
        private readonly I18NService I18N = new I18NService();

        protected void Page_Load(object sender, EventArgs e) {

            if (!Common.IsUserLoggedIn(Session))
                Response.Redirect("Default.aspx");

            UserService userService = new UserService();
            MyTeamsService myTeamsService = new MyTeamsService();
            RatingService ratingService = new RatingService();

            Common.FillI18NStrings(this, Session);
            int uid = Common.GetUserId(Session);

            userRoleField.Value = userService.GetUserRole(uid) +"";

            String option = Request.QueryString["opId"];
            if(option == "leave"){
                int teamId = int.Parse(Request.QueryString["teamId"]);
                myTeamsService.DelUserFromTeam(uid, teamId);
            }else if(option == "del"){
                int userId = int.Parse(Request.QueryString["Uid"]);
                int teamId = int.Parse(Request.QueryString["teamId"]);
                myTeamsService.DelUserFromTeam(userId, teamId);
            }

            TeamBean[] teamList;
            if (userRoleField.Value == "0")
            {
                teamList = myTeamsService.GetListOfMyTeams(Common.GetUserId(Session));
            }else{
                teamList = myTeamsService.GetTutorTeams(Common.GetUserId(Session));
            }

            for (int i = 0; i < teamList.Length; i++)
            {
                TableRow tr = new TableRow();
                
                TableCell tc = new TableCell();
                tc.Text = "<a href=\"?teamOId=" + i + "\">" + teamList[i].projectName + "</a>";
                tc.HorizontalAlign = HorizontalAlign.Left;
                tr.Cells.Add(tc);
                TeamListMenu.Controls.Add(tr);
            }
           
            
            String teamOId = Request.QueryString["teamOId"];
            if (teamOId == null || option=="leave")
            {
                /* DISPLAY NEWS */
                updates.Visible = true;
                #region Generate RSS
            var rss = new Rss20FeedFormatter();
            String url = String.Format("http://localhost:1059/RssGenerator.aspx?Uid={0}&type=members", uid);

                rss.ReadFrom(XmlReader.Create(url,
                                              new XmlReaderSettings {
                                                  IgnoreWhitespace = true,
                                                  CheckCharacters = true,
                                                  CloseInput = true,
                                                  IgnoreComments = true,
                                                  IgnoreProcessingInstructions = true,
                                                  ProhibitDtd = false
                                              }));

                int count = rss.Feed.Items.Count();

                if (count > 0)
                {
                    var members = from i in rss.Feed.Items
                                  orderby i.PublishDate descending
                                  select new { FeedTitle = i.Title.Text, FeedContent = i.Summary.Text, FeedLink = i.Links[0].Uri };

                    rptMembers.DataSource = members;
                    rptMembers.DataBind(); 
                }
                #endregion
            }
            else
            {
                /* DISPLAY SELECTED TEAM */
                #region TeamDetail
                int teamIndx = int.Parse(teamOId);
                teamOIDHF.Value = teamOId;
                if(teamIndx<teamList.Length && teamIndx>=0){
                    List<TeamBean> tmp = new List<TeamBean>();
                    tmp.Add(teamList[teamIndx]);
                    teamIdHF.Value = teamList[teamIndx].teamId.ToString();
                    ShowMyTeams.DataSource = tmp;
                    ShowMyTeams.DataBind();

                    updates.Visible = false;
                }
                #endregion
            }
            

            
        }

    }
}